package cn.itzd.filter;

import cn.itzd.enums.AppHttpCodeEnum;
import cn.itzd.utils.result.R;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.PrintWriter;

//没有权限
@Service
public class MyAccessDeniedHandler implements AccessDeniedHandler {

    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json");
        PrintWriter writer = response.getWriter();
        R<Object> r = new R<>();
        r.setMsg(AppHttpCodeEnum.NO_OPERATOR_AUTH.getMsg());
        r.setCode(AppHttpCodeEnum.NO_OPERATOR_AUTH.getCode());
        writer.write(new ObjectMapper().writeValueAsString(r));
        writer.flush();
        writer.close();
    }

}
